#!/bin/bash

web_server_dir="/opt/webServerDir"
web_server_port="8000"

[ -d $web_server_dir ] || mkdir -p $web_server_dir

py2_cmd=`which python2 2>/dev/null`
[ "$?" != "0" ] && py3_cmd=`which python3 2>/dev/null`

case $1 in
  start)
    cd ${web_server_dir}
    if [ -n "$py2_cmd" ]
    then
        nohup $py2_cmd -m SimpleHTTPServer ${web_server_port} > /var/log/simple_http_server.log 2>&1 &
    elif [ -n "$py3_cmd" ]
    then
        nohup $py3_cmd -m http.server ${web_server_port} > /var/log/simple_http_server.log 2>&1 &
    else
        echo "No python environment!"
    fi
;;
  stop)
    ps aux | grep -E "SimpleHTTPServer|http.server" | grep -v "grep" | awk '{print $2}' | xargs kill -9
esac
